package me.shiqiang.im.action;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.tst.common.returnobj.BaseReturnObj;
import com.tst.common.returnobj.ReturnObj;
import com.tst.common.returnobj.ReturnObjCode;
import me.shiqiang.im.domain.Group;
import me.shiqiang.im.domain.GroupUser;
import io.swagger.annotations.Api;
import me.shiqiang.im.service.GroupService;
import me.shiqiang.im.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cuishiqiang
 * @date 2017/12/8 上午11:48
 */
@Api(value = "/im/group", description = "群相关接口")
@RestController
@RequestMapping("im/group")
public class GroupAction {

    @Autowired
    GroupService groupService;

    @GetMapping("{groupId}")
    public ReturnObj get(@PathVariable Long groupId) {
        GroupVo groupVo = groupService.getFormalizedGroup(groupId);
        return new ReturnObj<>(ReturnObjCode.SUCCESS,groupVo);
    }

    @GetMapping("{groupId}/getGroup/{version}")
    public GroupChangeResultVo get(@PathVariable Long groupId, @PathVariable long version) {
        return groupService.getGroup(groupId,version);
    }

    @GetMapping("{groupId}/users")
    public ReturnObj<Collection<GroupUser>> getGroupUsers(@PathVariable Long groupId) {
        System.out.println(groupId+"发起请求");
        Group group = groupService.get(groupId);
        if (group == null) {
            return new ReturnObj<>(ReturnObjCode.SUCCESS,Collections.emptyList());
        }
        List<GroupUser> groupUsers = group.getUsers().values().stream().map(groupUser -> {
            groupUser.setNickName(groupUser.getNickName() == null ? "" : groupUser.getNickName());
            return groupUser;
        }).collect(Collectors.toList());
        return new ReturnObj<>(ReturnObjCode.SUCCESS,groupUsers);
    }

    @PostMapping("new")
    public ReturnObj<Long> newGroup(@RequestBody NewGroupVo newGroupVo) {
        try {
            Long groupId = groupService.newGroup(newGroupVo);
            return new ReturnObj<>(ReturnObjCode.SUCCESS, groupId);
        } catch (Exception e) {
            return new ReturnObj<>(ReturnObjCode.UNKOWN_ERROR, 0L);
        }
    }

    @PutMapping("{groupId}/mute/{userId}")
    public BaseReturnObj mute(@PathVariable Long groupId, @PathVariable Long userId) {
       return groupService.mute(groupId, userId);
    }

    @PutMapping("{groupId}/mute/{userId}/{targetId}")
    public BaseReturnObj mute(@PathVariable Long groupId, @PathVariable Long userId, @PathVariable Long targetId) {
        return groupService.mute(groupId, userId, targetId);
    }

    @PutMapping("{groupId}/unmute/{userId}")
    public BaseReturnObj unmute(@PathVariable Long groupId, @PathVariable Long userId) {
       return groupService.unmute(groupId, userId);
    }

    @PutMapping("{groupId}/unmute/{userId}/{targetId}")
    public BaseReturnObj unmute(@PathVariable Long groupId, @PathVariable Long userId, @PathVariable Long targetId) {
        return groupService.unmute(groupId, userId, targetId);
    }

    @PutMapping("/modifyGroupName")
    public BaseReturnObj modifyGroupName(@RequestBody ModifyGroupPropsVo vo) {
        return groupService.modifyGroupName(vo);
    }

    @PutMapping("/modifyGroupAvatar")
    public BaseReturnObj modifyGroupAvatar(@RequestBody ModifyGroupPropsVo vo) {
       return groupService.modifyGroupAvatar(vo);
    }

    @PutMapping("/modifyGroupAnnouncement")
    public BaseReturnObj modifyGroupAnnouncement(@RequestBody ModifyGroupPropsVo vo) {
       return groupService.modifyGroupAnnouncement(vo);
    }

    @PutMapping("/modifyGroupBgI")
    public BaseReturnObj modifyGroupBgI(@RequestBody ModifyGroupPropsVo vo) {
       return groupService.modifyGroupBgI(vo);
    }


    @PutMapping("{groupId}/addGroupUser")
    public BaseReturnObj addGroupUser(@PathVariable Long groupId,@RequestBody NewGroupVo newGroupVo) throws JsonProcessingException {
        groupService.addGroupUser(groupId,newGroupVo);
        return new BaseReturnObj(ReturnObjCode.SUCCESS,"添加用户成功");
    }


    @PutMapping("{userId}/sendAddNoticeTo/{groupId}")
    public BaseReturnObj sendAddNotice(@PathVariable Long userId, @PathVariable Long groupId,@RequestBody List<Long> ids) throws JsonProcessingException {
        groupService.sendAddMemberNotice(userId,groupId,ids);
        return new BaseReturnObj(ReturnObjCode.SUCCESS,"成功");
    }

    @PutMapping("{userId}/sendDelNoticeTo/{groupId}")
    public BaseReturnObj sendDelNotice(@PathVariable Long userId, @PathVariable Long groupId,@RequestBody List<Long> ids) throws JsonProcessingException {
        groupService.sendDelMemberNotice(userId,groupId,ids);
        return new BaseReturnObj(ReturnObjCode.SUCCESS,"成功");
    }

    @PutMapping("/deleteGroupUser")
    public BaseReturnObj deleteGroupUser(@RequestBody DeleteGroupUserVo vo) throws JsonProcessingException {
       return groupService.deleteGroupUser(vo);
    }

    /**
     * 用户退出群聊
     * @param groupId
     * @param userId
     * @throws JsonProcessingException
     */
    @DeleteMapping("{groupId}/{userId}")
    public BaseReturnObj quit(@PathVariable Long groupId,@PathVariable Long userId) throws JsonProcessingException {
        groupService.quit(groupId,userId);
        return new BaseReturnObj(ReturnObjCode.SUCCESS,"成功");
    }

    /**
     * 用户修改群昵称
     * @param nickNameVo
     * @return
     */
    @PutMapping("nickName")
    public BaseReturnObj setNickName(@RequestBody NickNameVo nickNameVo) {
        return groupService.setNickName(nickNameVo);
    }

    @GetMapping("{groupId}/isMuted/{userId}")
    public boolean isMuted(@PathVariable Long groupId,@PathVariable Long userId) {
       return groupService.isMuted(groupId,userId);
    }

    @GetMapping("{groupId}/allMutedAndIsNormal/{userId}")
    public boolean allMutedAndIsNormal(@PathVariable Long groupId,@PathVariable Long userId) {
        return groupService.allMutedAndIsNormal(groupId,userId);
    }

    @PutMapping("{groupId}/{userId}/assignAdmin/{targetId}")
    public BaseReturnObj assignAdmin(@PathVariable Long groupId, @PathVariable Long userId, @PathVariable Long targetId) {
        return groupService.assignAdmin(groupId, userId, targetId);
    }

    @PutMapping("{groupId}/{userId}/relieveAdmin/{targetId}")
    public BaseReturnObj relieveAdmin(@PathVariable Long groupId, @PathVariable Long userId, @PathVariable Long targetId) {
        return groupService.relieveAdmin(groupId, userId, targetId);
    }


    /**
     * 消息免打扰
     * @param userId
     * @param sessionId
     * @param ctype
     * @return
     */
    @PutMapping("{userId}/noDisturb/{sessionId}/{ctype}")
    public BaseReturnObj noDisturb(@PathVariable Long userId, @PathVariable Long sessionId, @PathVariable int ctype) {
        return groupService.noDisturb(userId, sessionId, ctype);
    }

    @PutMapping("{userId}/cancelNoDisturb/{sessionId}/{ctype}")
    public BaseReturnObj cancelNoDisturb(@PathVariable Long userId, @PathVariable Long sessionId, @PathVariable int ctype) {
        return groupService.cancelNoDisturb(userId, sessionId, ctype);
    }

    @GetMapping("{userId}/isNoDisturb/{sessionId}/{ctype}")
    public boolean isNoDisturb(@PathVariable Long userId, @PathVariable Long sessionId, @PathVariable int ctype) {
        return groupService.isNoDisturb(userId, sessionId, ctype);
    }



    @DeleteMapping("/{groupId}/deleteGroup")
    public void deleteGroup(@PathVariable Long groupId) throws JsonProcessingException {
        groupService.deleteGroup(groupId);
    }

//    @PutMapping("/incremental")
//    public BaseReturnObj saveIncrementalLog(@RequestBody GroupChangeLogVo groupChangeLogVo) {
////        groupService.saveIncrementalLog(groupChangeLogVo);
//        return new BaseReturnObj(ReturnObjCode.SUCCESS, "保存增量log成功");
//    }


    @PostMapping("serviceGroup/new")
    public ReturnObj<Long> newServiceGroup(@RequestBody NewServiceGroupVo newGroupVo) {
        try {
            Long groupId = groupService.newGroup(newGroupVo);
            return new ReturnObj<>(ReturnObjCode.SUCCESS, groupId);
        } catch (Exception e) {
            return new ReturnObj<>(ReturnObjCode.UNKOWN_ERROR, 0L);
        }
    }


    @PostMapping("mqMsg/{groupId}")
    public void newServiceGroup(@PathVariable Long groupId) {
        groupService.sendMqMsg(groupId.toString());
    }

    @GetMapping("/groupInfo/{platformId}")
    public ReturnObj getAllGroupInfoByPlatformId(@PathVariable Long platformId) {
        return new ReturnObj(ReturnObjCode.SUCCESS, "",groupService.getAllGroupInfoByPlatformId(platformId));
    }

}
